<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\ExportButton;
use App\Admin\Repositories\AnswerSurveyLog;
use App\Models\AnswerSurveyOption;
use App\Models\AnswerSurveySubject;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\EasyExcel\Excel;
use think\facade\Request;

class AnswerSurveyLogController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $request = request();

        $answer_id = $request->get('answer_id');

        return Grid::make(AnswerSurveyLog::with(['user', 'answer', 'subject', 'option']), function (Grid $grid) use ($answer_id) {
            $grid->model()->orderBy('id', 'desc');

            $grid->column('id')->sortable();
            $grid->column('user.name', admin_trans('answer-survey-log.fields.user_name'));
            $grid->column('answer.title', admin_trans('answer-survey-log.fields.answer_title'));
            $grid->column('subject.title', admin_trans('answer-survey-log.fields.subject_title'));
            $grid->column('option.serial', admin_trans('answer-survey-log.fields.serial'));
//            $grid->column('score');
            $grid->column('created_at')->sortable();

            $grid->tools(new ExportButton('答题/调查统计', $answer_id, 1, '统计'));
            $grid->tools(new ExportButton('答题/调查建议', $answer_id, 2, '建议'));

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
            });

            $grid->disableDeleteButton();
            $grid->disableCreateButton();
            $grid->disableEditButton();
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, AnswerSurveyLog::with(['user', 'answer', 'subject', 'option']), function (Show $show) {
            $show->field('id');
            $show->field('user.name', admin_trans('answer-survey-log.fields.user_name'));
            $show->field('answer.title', admin_trans('answer-survey-log.fields.answer_title'));
            $show->field('subject.title', admin_trans('answer-survey-log.fields.subject_title'));
//            $show->field('score');
            if (!$show->model()->option_id) {
                $show->field('content');
            } else {
                $show->field('option.serial', admin_trans('answer-survey-log.fields.serial'));
            }

            $show->field('created_at');
            $show->field('updated_at');
        });
    }


    /**
     * 导出数据
     */
    public function export() {
        $answer_id = request()->get('answer_id');
        $title = request()->get('title');
        $type = request()->get('type');

        if ($type == 1) {
            $subject = AnswerSurveySubject::where('answer_id', $answer_id)
                ->where('type', '!=', 3)
                ->get()
                ->toArray();

            $new_arr = [];
            $i = 0;

            foreach ($subject as $key => &$value) {
                $total = \App\Models\AnswerSurveyLog::where('answer_id', $answer_id)
                    ->where('option_id', '!=', 0)
                    ->where('subject_id', $value['id'])
                    ->get()
                    ->count();

                $option = AnswerSurveyOption::where('subject_id', $value['id'])
                    ->get()
                    ->toArray();

                foreach ($option as $k => &$v) {
                    $v['serial_count'] = \App\Models\AnswerSurveyLog::where('option_id', $v['id'])
                        ->get()
                        ->count();
                    $v['percentage'] = round(($v['serial_count'] / $total) * 100);

                    $new_arr[$i]['id1'] = $value['title'];
                    $new_arr[$i]['id2'] = $v['serial'];
                    $new_arr[$i]['id3'] = $v['percentage'].'%';
                    $new_arr[$i]['id4'] = $v['serial_count'];

                    $i ++;
                }

                $value['option'] = $option;
            }

            $header = [
                'id1' => '题目',
                'id2' => '选项',
                'id3' => '比例',
                'id4' => '人数'
            ];
        } else {
            $subject = AnswerSurveySubject::where('answer_id', $answer_id)
                ->where('type', '=', 3)
                ->get()
                ->toArray();

            $new_arr = [];
            $i = 0;

            foreach ($subject as $key => $value) {
                $log = \App\Models\AnswerSurveyLog::where('subject_id', $value['id'])
                    ->get()
                    ->toArray();

                foreach ($log as $k => $v) {
                    $new_arr[$i]['id1'] = $value['title'];
                    $new_arr[$i]['id2'] = $v['content'];

                    $i ++;
                }
            }

            $header = [
                'id1' => '题目',
                'id2' => '建议'
            ];
        }

        Excel::export($new_arr)->headings($header)->download("{$title}.xlsx");
    }
}
